package ru.svi.bluetooth;

public class MessageParser {
	private String message;

	public MessageParser(String message) {
		this.message = message;
	}

	public Measure[] getMeasures(String[] interesNames) {
		Measure[] measures = new Measure[interesNames.length];
		for (int k = 0; k < interesNames.length; k++) {
			measures[k] = new Measure();
			measures[k].setName(interesNames[k]);
		}

		String[] lines = message.split("\r\n");
		String[] names = lines[lines.length - 4].replace("> ", "").split(",");
		String[] units = lines[lines.length - 3].split(",");
		String[] curValues = lines[lines.length - 2].split(",");
		for (int j = 0; j < interesNames.length; j++) {
			for (int k = 0; k < names.length; k++)
				if (names[k].equals(interesNames[j])) {
					measures[j].setUnit(units[k]);
					if (curValues[k].equals("---"))
						measures[j].setCurValue(0D);
					else
						measures[j].setCurValue(Double.valueOf(curValues[k]));
					break;
				}

		}

		return measures;
	}

}
